﻿using BaseDal;
using Common;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace BaseBll
{
    public class AssetInvBll
    {
        private static readonly Lazy<AssetInvBll> dal = new Lazy<AssetInvBll>(() => new AssetInvBll());
        public static AssetInvBll Instance { get; } = dal.Value;

        // 获取单例实例
        readonly AssetInvDal DalInstance;

        private AssetInvBll()
        {
            DalInstance = AssetInvDal.Instance;
        }



        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool IsExist(string strWhere)
        {
            return DalInstance.IsExist(strWhere);
        }

        /// <summary>
        /// 查询分页数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strWhere"></param>
        /// <param name="filedOrder"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public DataTable GetPageList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return DalInstance.GetPageList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<AssetHead> GetList(string strWhere)
        {
            return DalInstance.GetList(strWhere);
        }

        /// <summary>
        /// 根据id查询实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AssetHead GetModelById(int id)
        {
            return DalInstance.GetModelById(id);
        }

        /// <summary>
        /// 根据资产编号查询实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AssetHead GetModelByNo(string assetNo)
        {
            return DalInstance.GetModelByNo(assetNo);
        }

        /// <summary>
        /// 根据主表ID查询所有附属资产
        /// </summary>
        /// <param name="headID"></param>
        /// <returns></returns>
        public List<AssetBody> GetAssetBodyList(decimal headID)
        {
            return DalInstance.GetAssetBodyList(headID);
        }

        /// <summary>
        /// 查询处于维护提醒时间内的资产
        /// </summary>
        /// <returns></returns>
        public List<AssetHead> GetKeepTimeWarnAsset()
        {
            return DalInstance.GetKeepTimeWarnAsset();
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool AddAsset(AssetHead model)
        {
            return DalInstance.AddAsset(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateAsset(AssetHead model)
        {
            return DalInstance.UpdateAsset(model);
        }

        /// <summary>
        /// 删除一条数据，及子表所有相关数据
        /// </summary>
        public bool DeleteAsset(int ID)
        {
            return DalInstance.DeleteAsset(ID);
        }

        /// <summary>
        /// 修改资产状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UpdateStatus(decimal id, int status)
        {
            return DalInstance.UpdateStatus(id, status);
        }

        /// <summary>
        /// 更新资产的下个维护周期
        /// </summary>
        /// <param name="assetNo"></param>
        /// <returns></returns>
        public int UpdAsset_MCycle(string assetNo)
        {
            var res = 0;
            // 获取资产信息
            AssetHead model = GetModelByNo(assetNo);
            if (model != null)
            {
                model.MaintainTimeStart = model.MaintainTimeEnd.AddDays(1);
                // 将维护周期（月） 转为天数
                double days = Utils.ConvertMonthToDays(DateTime.Now, model.M_Cycle);
                // 将天数转换为TimeSpan
                TimeSpan timeSpanToAdd = TimeSpan.FromDays(days);

                // 计算维护结束时间
                model.MaintainTimeEnd = model.MaintainTimeStart.Add(timeSpanToAdd);
                res = DalInstance.UpdAsset_MCycle(model.ID, model.MaintainTimeStart, model.MaintainTimeEnd);
            }
            return res;
        }

        /// <summary>
        /// 验证model各字段合法性
        /// </summary>
        /// <param name="model"></param>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        public bool VerifyModel(AssetHead model, ref string msg)
        {
            if (string.IsNullOrEmpty(model.AssetNo))
            {
                msg = "请填写资产编号";
                return false;
            }
            if (string.IsNullOrEmpty(model.AssetName))
            {
                msg = "请填写资产名称";
                return false;
            }
            string strWhere;
            if (model.ID <= 0)
            {
                strWhere = " and AssetNo='" + model.AssetNo + "'";
                if (IsExist(strWhere))
                {
                    msg = "资产编号已存在";
                    return false;
                }
            }

            // 根据人员ID获取人员姓名
            Staff staff = StaffBll.Instance.GetModelById(model.StaffID);
            if (staff != null)
            {
                model.StaffName = staff.Name;
            }
            else
            {
                msg = "所选人员不存在";
                return false;
            }
            // 根据typeID获取typeName
            ItemType type = ItemBll.Instance.GetTypeModelById(model.TypeID);
            if (type != null)
            {
                model.TypeName = type.TypeName;
            }
            else
            {
                msg = "所选类别不存在";
                return false;
            }

            // 获取部门名称
            Department dept = DeptBll.Instance.GetModelById(model.DeptID);
            if (dept != null)
            {
                model.DeptName = dept.FullName;
            }
            else
            {
                msg = "所选部门不存在";
                return false;
            }

            // 获取供应商名称
            Supplier supplier = SupplierBll.Instance.GetModelById(model.SupplierID);
            if (supplier != null)
            {
                model.SupplierName = supplier.S_Name;
            }
            else
            {
                msg = "所选供应商不存在";
                return false;
            }

            // 维护结束时间
            if (model.MaintainTimeStart != null && model.MaintainTimeStart != DateTime.MinValue && model.M_Cycle > 0)
            {
                // 将维护周期（月） 转为天数
                double days = Utils.ConvertMonthToDays(DateTime.Now, model.M_Cycle);
                // 将天数转换为TimeSpan
                TimeSpan timeSpanToAdd = TimeSpan.FromDays(days);

                // 计算维护结束时间
                model.MaintainTimeEnd = model.MaintainTimeStart.Add(timeSpanToAdd);
            }

            return true;
        }


    }
}